Page({
  data: {
    banners: [
      { 
        id: 1, 
        imageUrl: '/assets/images/1.jpg'
      },
      { 
        id: 2, 
        imageUrl: '/assets/images/2.jpg'
      },
      { 
        id: 3, 
        imageUrl: '/assets/images/3.jpg'
      }
    ],
    news: [
      {
        id: 1,
        tag: '新品',
        title: '小麦秸秆打包机新品上市'
      },
      {
        id: 2,
        tag: '活动',
        title: '秸秆回收季，补贴大放送'
      },
      {
        id: 3,
        tag: '促销',
        title: '秸秆饲料制作设备优惠活动'
      },
      {
        id: 4,
        tag: '资讯',
        title: '秸秆综合利用新政策解读'
      }
    ],
    categories: [
      { id: 1, name: '小麦秸秆', icon: 'success', color: '#FFB74D', imageUrl: '/assets/images/1.jpg' },
      { id: 2, name: '玉米秸秆', icon: 'success', color: '#4CAF50' ,imageUrl: '/assets/images/2.jpg'},
      { id: 3, name: '花生秧', icon: 'success', color: '#FF7043',imageUrl: '/assets/images/3.jpg' },
      { id: 4, name: '大豆秸秆', icon: 'success', color: '#42A5F5' ,imageUrl: '/assets/images/1.jpg'},
      { id: 5, name: '饲料', icon: 'success', color: '#7E57C2',imageUrl: '/assets/images/1.jpg' }
    ],
    products: [
      {
        id: 1,
        title: '优质小麦秸秆',
        price: 280,
        unit: '吨',
        location: '山东',
        imageUrl: '/assets/images/1.jpg',
        isPresale: true
      },
      {
        id: 2,
        title: '玉米秸秆饲料',
        price: 320,
        unit: '吨',
        location: '河南',
        imageUrl: '/assets/images/2.jpg'
      },
      {
        id: 3,
        title: '花生秧打包',
        price: 260,
        unit: '吨',
        location: '河北',
        imageUrl: '/assets/images/3.jpg'
      },
      {
        id: 4,
        title: '大豆秸秆',
        price: 300,
        unit: '吨',
        location: '黑龙江',
        imageUrl: '/assets/images/1.jpg'
      },
      {
        id: 5,
        title: '秸秆饲料颗粒',
        price: 1200,
        unit: '吨',
        location: '内蒙古',
        imageUrl: '/assets/images/1.jpg',
        isPresale: true
      },
      {
        id: 6,
        title: '小麦秸秆打包',
        price: 250,
        unit: '吨',
        location: '安徽',
        imageUrl: '/assets/images/1.jpg'
      },
      {
        id: 7,
        title: '玉米秸秆青储',
        price: 340,
        unit: '吨',
        location: '吉林',
        imageUrl: '/assets/images/1.jpg'
      },
      {
        id: 8,
        title: '花生秧饲料',
        price: 380,
        unit: '吨',
        location: '山西',
        imageUrl: '/assets/images/2.jpg'
      }
    ]
  },

  onLoad() {
    this.loadProducts()
  },

  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  },

  // 搜索事件处理
  onSearch(e) {
    const keyword = e.detail.value
    console.log('搜索关键词：', keyword)
    // TODO: 实现搜索逻辑
  },

  // 分类点击事件
  onCategoryTap(e) {
    const categoryId = e.currentTarget.dataset.id
    // TODO: 根据分类筛选商品
    console.log('选择分类：', categoryId)
  },

  // 商品点击事件
  onProductTap(e) {
    const productId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/detail/detail?id=${productId}`
    })
  },

  // 查看更多新闻
  onMoreNews() {
    wx.navigateTo({
      url: '/pages/news/list'
    })
  },

  // 查看新闻详情
  onNewsDetail(e) {
    const newsId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/news/detail?id=${newsId}`
    })
  },

  // 加载商品数据
  loadProducts() {
    // TODO: 从服务器加载商品数据
    console.log('加载商品数据')
  }
})
